package com.foreknow.view;

import com.foreknow.model.Business;
import com.foreknow.service.impl.BusinessServiceImpl;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class MainWindow extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private BusinessTableModel businessTable;
	private JTable jTable;
	private JTextField deleteFiled;

	private BusinessServiceImpl businessServiceImpl = new BusinessServiceImpl();
	private List<Business> list;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(10, 10, 123, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		JButton Query = new JButton("\u67E5\u8BE2");
		Query.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				BusinessServiceImpl businessService = new BusinessServiceImpl();
				String name = textField.getText();
				list = businessService.getQueryByName(name);
				businessTable.setData(list);
			}
		});
		Query.setBounds(148, 9, 97, 23);
		contentPane.add(Query);

		
		JButton Add = new JButton("\u6DFB\u52A0");
		Add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Add add = new Add();

			}
		});
		Add.setBounds(46, 212, 97, 23);
		contentPane.add(Add);
		
		JButton Update = new JButton("\u4FEE\u6539");
		Update.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Update update = new Update();
			}
		});
		Update.setBounds(170, 212, 97, 23);
		contentPane.add(Update);
		
		JButton Delete = new JButton("\u5220\u9664");
		Delete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int numrow = jTable.getSelectedRow();
				//获取当前行
				Business business = list.get(numrow);
				//删除所选行
				businessServiceImpl.deleteById(business.getBusinessId());
				BusinessTableModel businessTable = new BusinessTableModel();
				businessTable.setData(list);
				MainWindow.this.dispose();
				System.out.println("删除成功");
				MainWindow mainWindow = new MainWindow();
			}
		});
		Delete.setBounds(293, 212, 97, 23);
		contentPane.add(Delete);

		businessTable = new BusinessTableModel();
		list = businessServiceImpl.getQueryAll();
		businessTable.setData(list);
		jTable = new JTable(businessTable);
		jTable.setBackground(Color.WHITE);
		contentPane.add(jTable);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(25, 40, 387, 124);
		scrollPane.setViewportView(jTable);
		contentPane.add(scrollPane);

		this.setVisible(true);
	}
}
